<template>
  <view class="main-container">
    <!-- 视频流区域 -->
    <view class="video-container" @touchstart="handleVideoTouch" @touchmove="handleVideoZoom"
      @click="showControlsWithDelay" @mousemove="showControlsWithDelay" @touchmove.stop="showControlsWithDelay">
      <video id="mainVideo" class="video-stream" :src="videoUrl" autoplay loop muted :controls="false"
        object-fit="cover"></video>

      <!-- 顶部状态栏 -->
      <view class="status-bar">
        <view class="settings-icon" @click="toggleSettingsPanel">
          <image src="/static/icons/settings.svg" mode="aspectFit" />
        </view>
        <view class="status-item connection-status" :class="{ 'connected': isConnected }">
          <image src="/static/icons/signal.svg" mode="aspectFit" />
        </view>
      </view>

      <!-- 设置面板 -->
      <view class="modal-overlay" v-if="showSettingsPanel" @click="toggleSettingsPanel"></view>
      <view class="settings-panel" :class="{ 'show': showSettingsPanel }">
        <view class="panel-header">
          <text>{{ currentSettingsSection }}设置</text>
          <view class="close-btn" @click="toggleSettingsPanel">
            <text>×</text>
          </view>
        </view>
        <view class="panel-content">
          <view class="settings-section" v-if="currentSettingsSection === '云台相机'">
            <view class="setting-item">
              <text>彩色相机</text>
              <switch :checked="featureEnabled.colorCamera" @change="toggleFeature('colorCamera')" />
            </view>
            <view class="setting-item">
              <text>黑白相机</text>
              <switch :checked="featureEnabled.bwCamera" @change="toggleFeature('bwCamera')" />
            </view>
            <view class="setting-item">
              <text>测温功能</text>
              <switch :checked="featureEnabled.tempMeasure" @change="toggleFeature('tempMeasure')" />
            </view>
            <view class="setting-item">
              <text>云台巡航</text>
              <switch :checked="featureEnabled.ptzCruise" @change="toggleFeature('ptzCruise')" />
            </view>
          </view>
          <view class="settings-section" v-if="currentSettingsSection === '四合一'">
            <view class="setting-item">
              <text>语音对讲</text>
              <switch :checked="featureEnabled.intercom" @change="toggleFeature('intercom')" />
            </view>
            <view class="setting-item">
              <text>探照灯</text>
              <switch :checked="featureEnabled.spotlight" @change="toggleFeature('spotlight')" />
            </view>
            <view class="setting-item">
              <text>报警灯</text>
              <view class="alarm-light-controls">
                <switch :checked="alarmLight.enabled" @change="toggleAlarmLight" />
                <picker v-if="alarmLight.enabled" :value="alarmLight.mode" :range="['正常', '快速', '慢速']"
                  @change="changeAlarmMode">
                  <text>{{ alarmLight.mode === 'normal' ? '正常' : alarmLight.mode === 'fast' ? '快速' : '慢速' }}</text>
                </picker>
              </view>
            </view>
            <view class="setting-item">
              <text>广播内容</text>
              <view class="broadcast-controls">
                <button @click="showBroadcastEditor">编辑广播</button>
              </view>
            </view>
          </view>
          <view class="settings-section" v-if="currentSettingsSection === '机器狗'">
            <view class="setting-item">
              <text>控制模式</text>
              <picker :value="robotMode" :range="['自动', '手动']" @change="changeRobotMode">
                <text>{{ robotMode === 'auto' ? '自动' : '手动' }}</text>
              </picker>
            </view>
            <view class="setting-item">
              <text>手动控制</text>
              <switch :checked="featureEnabled.robotControl" @change="toggleFeature('robotControl')"
                :disabled="robotMode === 'auto'" />
            </view>
          </view>
          <view class="settings-section" v-if="currentSettingsSection === '系统'">
            <view class="setting-item">
              <text>服务器地址</text>
              <input type="text" v-model="serverConfig.url" placeholder="请输入服务器地址" />
            </view>
            <view class="setting-item">
              <text>服务器端口</text>
              <input type="text" v-model="serverConfig.port" placeholder="请输入端口号" />
            </view>
          </view>
        </view>
        <view class="panel-footer">
          <view class="section-btn" :class="{ active: currentSettingsSection === '云台相机' }"
            @click="switchSettingsSection('云台相机')">
            <text>云台相机</text>
          </view>
          <view class="section-btn" :class="{ active: currentSettingsSection === '四合一' }"
            @click="switchSettingsSection('四合一')">
            <text>四合一</text>
          </view>
          <view class="section-btn" :class="{ active: currentSettingsSection === '机器狗' }"
            @click="switchSettingsSection('机器狗')">
            <text>机器狗</text>
          </view>
          <view class="section-btn" :class="{ active: currentSettingsSection === '系统' }"
            @click="switchSettingsSection('系统')">
            <text>系统</text>
          </view>
        </view>
      </view>

      <!-- 功能按钮组 -->
      <view class="camera-functions" :class="{ 'v-show': showControls}">
        <view class="function-btn" v-if="featureEnabled.colorCamera" @click="toggleFeature('colorCamera')" :class="{ 'active': featureEnabled.colorCamera }">
          <image src="/static/icons/camera-color.svg" mode="aspectFit" />
          <text>彩色相机</text>
        </view>
        <view class="function-btn" v-if="featureEnabled.bwCamera" @click="toggleFeature('bwCamera')" :class="{ 'active': featureEnabled.bwCamera }">
          <image src="/static/icons/camera-bw.svg" mode="aspectFit" />
          <text>黑白相机</text>
        </view>
        <view class="function-btn" v-if="featureEnabled.tempMeasure" @click="toggleFeature('tempMeasure')" :class="{ 'active': featureEnabled.tempMeasure }">
          <image src="/static/icons/temperature.svg" mode="aspectFit" />
          <text>测温功能</text>
        </view>
        <view class="function-btn" v-if="featureEnabled.ptzCruise" @click="toggleFeature('ptzCruise')" :class="{ 'active': featureEnabled.ptzCruise }">
          <image src="/static/icons/cruise.svg" mode="aspectFit" />
          <text>云台巡航</text>
        </view>
        <view class="function-btn" v-if="featureEnabled.spotlight" @click="toggleFeature('spotlight')" :class="{ 'active': featureEnabled.spotlight }">
          <image src="/static/icons/spotlight.svg" mode="aspectFit" />
          <text>探照灯</text>
        </view>
        <view class="function-btn" v-if="featureEnabled.intercom" @click="toggleFeature('intercom')" :class="{ 'active': featureEnabled.intercom }">
          <image src="/static/icons/intercom.svg" mode="aspectFit" />
          <text>语音对讲</text>
        </view>
      </view>

      <!-- 机器狗控制 -->
      <view class="control-pad robot-control" :class="{ 'v-show': showControls && featureEnabled.robotControl }">
        <view class="direction-pad">
          <view class="direction-row">
            <view class="direction-btn up" @touchstart="startRobotMove('up')" @touchend="stopRobotMove">
            </view>
          </view>
          <view class="direction-row">
            <view class="direction-btn left" @touchstart="startRobotMove('left')" @touchend="stopRobotMove">
            </view>
            <view class="direction-btn right" @touchstart="startRobotMove('right')" @touchend="stopRobotMove">
            </view>
          </view>
          <view class="direction-row">
            <view class="direction-btn down" @touchstart="startRobotMove('down')" @touchend="stopRobotMove">
            </view>
          </view>
        </view>
      </view>

      <!-- 摄像头控制 -->
      <view class="control-pad camera-control" :class="{ 'v-show': showControls}">
        <view class="direction-pad">
          <view class="direction-row">
            <view class="direction-btn up" @touchstart="startCameraMove('up')" @touchend="stopCameraMove">
            </view>
          </view>
          <view class="direction-row">
            <view class="direction-btn left" @touchstart="startCameraMove('left')" @touchend="stopCameraMove">
            </view>
            <view class="direction-btn right" @touchstart="startCameraMove('right')" @touchend="stopCameraMove">
            </view>
          </view>
          <view class="direction-row">
            <view class="direction-btn down" @touchstart="startCameraMove('down')" @touchend="stopCameraMove">
            </view>
          </view>
        </view>
      </view>

      <!-- 停止按钮 -->
      <!-- <view class="emergency-stop floating" @click="handleEmergencyStop">
        <text>停止</text>
      </view> -->
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      videoUrl: '/static/videos/test.mp4', // 替换为实际的视频流地址
      currentMode: 'visible', // visible, infrared, dual
      zoomLevel: 1,
      lastTouchDistance: 0,
      showSettingsPanel: false,
      showControls: true,
      controlsTimer: null,
      connectionTimer: null, // 连接状态切换定时器
      currentSettingsSection: '云台相机',
      isConnected: false, // 连接状态
      // 功能开关状态
      featureEnabled: {
        colorCamera: true, // 彩色相机
        bwCamera: true, // 黑白相机
        tempMeasure: true, // 测温功能
        ptzCruise: true, // 云台巡航
        intercom: true, // 语音对讲
        spotlight: false, // 探照灯
        intercom: false, // 语音对讲
        robotControl: true // 机器狗控制
      },
      // 报警灯设置
      alarmLight: {
        enabled: false,
        mode: 'normal', // normal, fast, slow
      },
      // 广播内容列表
      broadcastList: [],
      // 机器狗模式
      robotMode: 'auto', // auto, manual
      // 服务器设置
      serverConfig: {
        url: '',
        port: ''
      }
    }
  },
  mounted() {
    // 从本地存储加载设置
    this.loadSettings()
    // 初始化时启动自动隐藏计时器
    this.startHideControlsTimer()
    // 启动连接状态模拟
    this.startConnectionSimulation()
    // 监听页面显示事件
    uni.$on('page-show', this.showControlsWithDelay)
  },
  beforeDestroy() {
    // 清除计时器和事件监听
    this.clearHideControlsTimer()
    this.clearConnectionSimulation()
    uni.$off('page-show', this.showControlsWithDelay)
  },
  methods: {
    // 视频流手势操作
    handleVideoTouch(e) {
      if (e.touches.length === 2) {
        const touch1 = e.touches[0]
        const touch2 = e.touches[1]
        this.lastTouchDistance = Math.sqrt(
          Math.pow(touch2.pageX - touch1.pageX, 2) +
          Math.pow(touch2.pageY - touch1.pageY, 2)
        )
      }
    },
    handleVideoZoom(e) {
      if (e.touches.length === 2) {
        const touch1 = e.touches[0]
        const touch2 = e.touches[1]
        const currentDistance = Math.sqrt(
          Math.pow(touch2.pageX - touch1.pageX, 2) +
          Math.pow(touch2.pageY - touch1.pageY, 2)
        )

        if (this.lastTouchDistance > 0) {
          if (currentDistance > this.lastTouchDistance) {
            this.zoomLevel = Math.min(this.zoomLevel + 0.1, 30)
          } else {
            this.zoomLevel = Math.max(this.zoomLevel - 0.1, 1)
          }
        }
        this.lastTouchDistance = currentDistance
      }
    },
    // 设置面板控制
    toggleSettingsPanel() {
      this.showSettingsPanel = !this.showSettingsPanel
    },
    // 模式切换
    switchMode(mode) {
      this.currentMode = mode
      // 实现模式切换逻辑
    },
    // 机器狗控制
    startRobotMove(direction) {
      // 实现机器狗移动控制逻辑
      console.log('机器狗开始移动:', direction)
    },
    stopRobotMove() {
      // 实现机器狗停止移动逻辑
      console.log('机器狗停止移动')
    },
    // 摄像头控制
    startCameraMove(direction) {
      // 实现摄像头移动控制逻辑
      console.log('摄像头开始移动:', direction)
    },
    stopCameraMove() {
      // 实现摄像头停止移动逻辑
      console.log('摄像头停止移动')
    },

    // 停止
    // 控制面板显示隐藏相关方法
    showControlsWithDelay() {
      this.showControls = true
      this.startHideControlsTimer()
    },
    startHideControlsTimer() {
      this.clearHideControlsTimer()
      // 10秒后隐藏
      // this.controlsTimer = setTimeout(() => {
      //   this.showControls = false
      // }, 10000)
    },
    clearHideControlsTimer() {
      if (this.controlsTimer) {
        clearTimeout(this.controlsTimer)
        this.controlsTimer = null
      }
    },
    handleEmergencyStop() {
      uni.showModal({
        title: '警告',
        content: '确认执行停止？',
        confirmColor: '#ff0000',
        success: (res) => {
          if (res.confirm) {
            // 执行停止逻辑
            uni.vibrateShort()
            console.log('执行停止')
          }
        }
      })
    },
    // 连接状态模拟
    startConnectionSimulation() {
      this.connectionTimer = setInterval(() => {
        this.isConnected = !this.isConnected
      }, 3000) // 每3秒切换一次状态
    },
    clearConnectionSimulation() {
      if (this.connectionTimer) {
        clearInterval(this.connectionTimer)
        this.connectionTimer = null
      }
    },
    // 切换功能开关状态
    toggleFeature(feature) {
      this.featureEnabled[feature] = !this.featureEnabled[feature]
      // 保存设置到本地存储
      uni.setStorageSync('featureEnabled', this.featureEnabled)
      console.log(`${feature}功能状态:`, this.featureEnabled[feature] ? '开启' : '关闭')
    },
    // 切换设置面板部分
    switchSettingsSection(section) {
      this.currentSettingsSection = section
      console.log('切换到设置部分:', section)
    },
    // 切换报警灯状态
    toggleAlarmLight(e) {
      this.alarmLight.enabled = e.detail.value
      // 保存设置到本地存储
      uni.setStorageSync('alarmLight', this.alarmLight)
      console.log('报警灯状态:', this.alarmLight.enabled ? '开启' : '关闭')
    },
    // 切换报警灯模式
    changeAlarmMode(e) {
      const modes = ['normal', 'fast', 'slow']
      this.alarmLight.mode = modes[e.detail.value]
      // 保存设置到本地存储
      uni.setStorageSync('alarmLight', this.alarmLight)
      console.log('报警灯模式:', this.alarmLight.mode)
    },
    // 显示广播内容编辑器
    showBroadcastEditor() {
      uni.navigateTo({
        url: '/pages/broadcast/editor'
      })
    },
    // 切换机器狗模式
    changeRobotMode(e) {
      this.robotMode = e.detail.value === 0 ? 'auto' : 'manual'
      if (this.robotMode === 'auto') {
        this.featureEnabled.robotControl = false
        // 保存功能开关状态
        uni.setStorageSync('featureEnabled', this.featureEnabled)
      }
      // 保存机器狗模式
      uni.setStorageSync('robotMode', this.robotMode)
      console.log('机器狗模式:', this.robotMode)
    },
    
    // 从本地存储加载设置
    loadSettings() {
      const storedFeatureEnabled = uni.getStorageSync('featureEnabled')
      const storedAlarmLight = uni.getStorageSync('alarmLight')
      const storedRobotMode = uni.getStorageSync('robotMode')
      
      if (storedFeatureEnabled) {
        this.featureEnabled = storedFeatureEnabled
      }
      if (storedAlarmLight) {
        this.alarmLight = storedAlarmLight
      }
      if (storedRobotMode) {
        this.robotMode = storedRobotMode
      }
    }
  }
}
</script>

<style lang="scss">
.main-container {
  width: 100vw;
  height: 100vh;
  background-color: #000;
  position: relative;
  overflow: hidden;
}

.video-container {
  width: 100%;
  height: 100%;
  position: relative;

  .video-stream {
    width: 100%;
    height: 100%;
    background-color: #000;
  }
}

.status-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background-color: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  z-index: 100;

  image {
    width: 24px;
    height: 24px;
    filter: brightness(0) invert(1);
  }

  .settings-icon {
    cursor: pointer;
  }

  .connection-status {
    display: flex;
    align-items: center;

    image {
      filter: #ff4d4f;
    }

    &.connected {
      image {
        filter: #52c41a;
      }
    }
  }
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 199;
}

.settings-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.9);
  width: 400px;
  max-height: 80vh;
  background-color: rgba(0, 0, 0, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  z-index: 200;

  &.show {
    opacity: 1;
    visibility: visible;
    transform: translate(-50%, -50%) scale(1);
  }

  .panel-header {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);

    text {
      color: #fff;
      font-size: 18px;
      font-weight: 500;
    }

    .close-btn {
      width: 30px;
      height: 30px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;

      text {
        color: #fff;
        font-size: 24px;
      }
    }
  }

  .panel-content {
    padding: 20px;
    max-height: calc(80vh - 130px);
    overflow-y: auto;

    .settings-section {
      margin-bottom: 20px;

      .setting-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 15px;
        padding: 10px;
        background-color: rgba(255, 255, 255, 0.1);
        border-radius: 8px;

        text {
          color: #fff;
          font-size: 14px;
        }

        .alarm-light-controls {
          display: flex;
          align-items: center;
          gap: 10px;

          picker {
            background-color: rgba(255, 255, 255, 0.1);
            padding: 4px 8px;
            border-radius: 4px;

            text {
              font-size: 12px;
            }
          }
        }

        .broadcast-controls {
          button {
            font-size: 12px;
            padding: 4px 8px;
            background-color: rgba(255, 255, 255, 0.2);
            border: none;
            border-radius: 4px;
            color: #fff;

            &:active {
              background-color: rgba(255, 255, 255, 0.3);
            }
          }
        }

        input {
          background-color: rgba(255, 255, 255, 0.1);
          border: none;
          border-radius: 4px;
          padding: 4px 8px;
          color: #fff;
          font-size: 12px;
          width: 150px;

          &::placeholder {
            color: rgba(255, 255, 255, 0.5);
          }
        }

        picker {
          background-color: rgba(255, 255, 255, 0.1);
          padding: 4px 8px;
          border-radius: 4px;
          width: 80px;
          text-align: center;

          text {
            font-size: 12px;
          }
        }
      }
    }
  }

  .panel-footer {
    display: flex;
    align-items: center;
    justify-content: space-around;
    padding: 15px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);

    .section-btn {
      padding: 8px 15px;
      border-radius: 20px;
      background-color: rgba(255, 255, 255, 0.1);
      cursor: pointer;
      transition: all 0.3s ease;

      text {
        color: #fff;
        font-size: 14px;
      }

      &.active {
        background-color: rgba(255, 255, 255, 0.2);
      }

      &:hover {
        background-color: rgba(255, 255, 255, 0.15);
      }
    }
  }
}

.camera-functions {
  position: absolute;
  left: 50%;
  bottom: 30px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: row;
  gap: 15px;
  z-index: 100;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  background-color: rgba(0, 0, 0, 0.4);
  padding: 10px 20px;
  border-radius: 30px;
  backdrop-filter: blur(10px);

  &.v-show {
    opacity: 1;
    visibility: visible;
  }

  .function-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    padding: 6px;
    background-color: rgba(0, 0, 0, 0.5);
    border-radius: 8px;
    cursor: pointer;
    min-width: 40px;
    transition: all 0.3s ease;

    &.active {
      background-color: rgba(64, 158, 255, 0.8);
    }

    image {
      width: 18px;
      height: 18px;
    }

    text {
      color: #fff;
      font-size: 10px;
      text-align: center;
    }
  }
}

.control-pad {
  position: absolute;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;

  &.v-show {
    opacity: 1;
    pointer-events: auto;
  }

  &.robot-control {
    left: 30px;
    bottom: 140px;
  }

  &.camera-control {
    right: 30px;
    bottom: 140px;
  }

  .direction-pad {
    display: flex;
    flex-direction: column;
    gap: 5px;

    .direction-row {
      display: flex;
      justify-content: center;
      gap: 50px;

      .direction-btn {
        width: 60px;
        height: 60px;
        border-radius: 30px;
        background-color: rgba(0, 0, 0, 0.5);
        display: flex;
        align-items: center;
        justify-content: center;

        image {
          width: 30px;
          height: 30px;
        }

        &:active {
          background-color: rgba(255, 255, 255, 0.2);
        }
      }
    }
  }
}

.floating-buttons {
  position: absolute;
  right: 20px;
  top: 60px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;

  &.v-show {
    opacity: 1;
    pointer-events: auto;
  }

  .function-btn {
    width: 50px;
    height: 50px;
    border-radius: 25px;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 5px;

    image {
      width: 24px;
      height: 24px;
      margin-bottom: 2px;
    }

    text {
      color: #fff;
      font-size: 10px;
    }

    &:active {
      background-color: rgba(255, 255, 255, 0.2);
    }
  }
}

.emergency-stop {
  &.floating {
    position: absolute;
    bottom: 140px;
    left: 50%;
    transform: translateX(-50%);
    width: 80px;
    height: 80px;
    border-radius: 40px;
    background-color: #ff0000;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;

    text {
      color: #fff;
      font-size: 16px;
      font-weight: bold;
    }

    &:active {
      background-color: #cc0000;
    }
  }
}
</style>